﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using System;
using UtilLib;
using static UtilPublic.Redis.ServerRedis;
using static UtilPublic.Serialization.Jsh;
using ServerBase.Other;
using static ServerBase.DataBaseRedis.RdsTxt;
using ServerBase.Service;
using System.Collections.Generic;
using System.Runtime.Serialization;
using ServerBase.DataBaseRedis;
using static ServerPublic.Data.PubDataMethodMgr;

namespace ServerPublic.Data
{
    /// <summary>
    /// 服务器数据 每个服务器都有
    /// </summary>
    public static class PubDataServerMgr
    {
        //游戏基础数据
        private static PubDataServer Datas = new PubDataServer();

        //存档
        public static void SaveData()
        {
            ObjkvSave(RdsDataServer, Datas);
        }

        //读取
        public static void LoadData()
        {
            if (!ObjKvLoad(RdsDataServer, out Datas))
            {
                //创建新数据
                Datas = new PubDataServer();
                SaveData();
                loger.Info($"创建 {BaseServerInfo.ServerZone} 数据 成功");
            }
        }

        //获取 时间组
        public static DateTime GetBaseDatetime(EDataServerDtKey key)
        {
            if (Datas.DictBaseDatetime.TryGetValue((int)key, out var value))
            {
                return value;
            }
            else
            {
                return UtilEnum.EpochDateTime;
            }
        }
        //修改 时间组
        public static DateTime SetBaseDatetime(EDataServerDtKey key, DateTime value)
        {
            Datas.DictBaseDatetime[(int)key] = value;
            return value;
        }
        //获取 字符串组
        public static string GetBaseString(EDataServerStringKey key)
        {
            if (Datas.DictBaseString.TryGetValue((int)key, out var value))
            {
                return value;
            }
            else
            {
                return "";
            }
        }
        //修改 字符串组
        public static string SetBaseString(EDataServerStringKey key, string value)
        {
            Datas.DictBaseString[(int)key] = value;
            return value;
        }

    }
    /// <summary>
    /// 游戏基础数据
    /// </summary>
    [DataContract]
    public class PubDataServer
    {
        //时间组
        [DataMember]
        public Dictionary<int, DateTime> DictBaseDatetime = new Dictionary<int, DateTime>();
        //字符串组
        [DataMember]
        public Dictionary<int, string> DictBaseString = new Dictionary<int, string>();
    }

    // 时间组 关键字
    public enum EDataServerDtKey
    {
        未知 = 0,
        日更新 = 1,
        日整点 = 2,
        日结束 = 3,
        日五点 = 11,
        周日零点更新 = 31,
        周一零点更新 = 32,
        月零点更新 = 51,
    }
    // 字符串组 关键字
    public enum EDataServerStringKey
    {
        未知 = 0,
    }
}